package com.gdust.hzx.manager.controller;

import com.gdust.hzx.manager.service.BrandService;
import com.gdust.spzx.common.log.annotation.Log;
import com.gdust.spzx.common.log.enums.OperatorType;
import com.gdust.hzx.model.entity.product.Brand;
import com.gdust.hzx.model.vo.common.Result;
import com.gdust.hzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: BrandController
 * Package: com.gdust.spzx.manager.controller
 * Description:品牌类控制器
 *
 * @Author syun
 * @Create 2024/12/1 22:14
 * @Version 1.0
 */
@RestController// @RestController: 专用于restful风格的注解 = @Controller (控制器) + @ResponseBody (方法返回值为json)
@RequestMapping(value = "/admin/product/brand")
public class BrandController {
    //注入品牌服务类
    @Autowired
    private BrandService brandService;

    /**
     * 使用pagehelper分页插件进行分页
     * 分页查询品牌
     *
     * @param page  当前页
     * @param limit 每页记录数
     * @return
     */
//    @Log(title = "品牌列表",businessType = 0,operatorType = OperatorType.MANAGE)
    @GetMapping("/{page}/{limit}")
    public Result<PageInfo<Brand>> findByPage(
            @PathVariable Integer page,
            @PathVariable Integer limit) {
        PageInfo<Brand> pageInfo = brandService.findPage(page, limit);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

//    //TODO 测试分页代码
//    @Log(title = "品牌列表",businessType = 0,operatorType = OperatorType.MANAGE)
//    @GetMapping("/{page}/{limit}")
//    public Result<PageInfo<Brand>> findByPage(@PathVariable Integer page, @PathVariable Integer limit){
//        // 调用服务层的分页查询方法
//        PageInfo<Brand> pageInfo  = brandService.findPage(page,limit);
//        // 返回包含分页数据的成功结果
//        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
//    }

    /**
     *
     *  品牌添加
     *  `@RequestBody`：注解实现接收http请求的json数据，将json转换为java对象；（JSON->java）
     * @param brand
     * @return
     */
    @Log(title = "添加品牌",businessType = 1,operatorType = OperatorType.MANAGE)
    @PostMapping("save")
    public Result save(@RequestBody Brand brand) {
        //自定义方法：品牌添加
        brandService.save(brand);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据id修改品牌
     * @param brand
     * @return
     */
    @Log(title = "修改品牌",businessType = 2,operatorType = OperatorType.MANAGE)
    @PutMapping("updateById")
    public Result updateById(@RequestBody Brand brand){
        //自定义方法：根据id修改品牌
        brandService.updateById(brand);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 删除指定品牌
     * @param id 品牌id
     * @return
     */
    @Log(title = "删除品牌",businessType = 3,operatorType = OperatorType.MANAGE)
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id){
        //自定义方法：删除指定品牌
        brandService.deleteById(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 列表查询接口
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        //自定义方法：列表查询
        List<Brand> list = brandService.findAll();
        return Result.build(list,ResultCodeEnum.SUCCESS);
    }


}
